package com.tanhua.dubbo.config;

import cn.hutool.core.collection.CollectionUtil;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.MovementTimeLine;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MongoAsyncService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *   发布动态异步处理
     * @param friends
     * @param id
     * @param timeMillis
     * @param movement
     */
    @Async
    public void MovementThread(List<Friend> friends, ObjectId id, long timeMillis, Movement movement){
        log.error("进入异步调用");
        if (CollectionUtil.isNotEmpty(friends)){
            for (Friend friend : friends) {
                MovementTimeLine timeLine = new MovementTimeLine();
                timeLine.setMovementId(id);
                timeLine.setCreated(timeMillis);
                timeLine.setUserId(movement.getUserId());
                timeLine.setFriendId(friend.getFriendId());
                mongoTemplate.insert(timeLine);

            }
        }

    }


}
